package com.golddaymicromall.activity.recharge;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.util.OrderNumber;
import com.golddaymicromall.util.SPUtils;

import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ZaiXianHuiKuanActivity extends BaseActivity implements View.OnClickListener {
    private TextView mzaixianhuikuanbianhao_random;
    private EditText mzaixianhuikuan_moneyNum;
    private TextView mzaixianhuikuan_date;
    private Button mzaixianhuikuan_addConfirm;
    String customerid;
    String moneyNum;
    String random;
    String deptFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zai_xian_hui_kuan);
        customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        deptFlag = SPUtils.get(getApplicationContext(), "deptFlag", "").toString();
        initView();
    }

    private void initView() {
        mzaixianhuikuanbianhao_random = (TextView) findViewById(R.id.zaixianhuikuanbianhao_random);
        if (deptFlag.equals("1")) {
            mzaixianhuikuanbianhao_random.setText("AS-" + OrderNumber.getNumber() + "-" + customerid);
        } else if (deptFlag.equals("2")) {
            mzaixianhuikuanbianhao_random.setText("ATS-" + OrderNumber.getNumber() + "-" + customerid);
        }
        random = mzaixianhuikuanbianhao_random.getText().toString();
        mzaixianhuikuan_moneyNum = (EditText) findViewById(R.id.zaixianhuikuan_moneyNum);
        mzaixianhuikuan_date = (TextView) findViewById(R.id.zaixianhuikuan_date);
        mzaixianhuikuan_date.setText(getCurrentDate());
        mzaixianhuikuan_addConfirm = (Button) findViewById(R.id.zaixianhuikuan_addConfirm);
        mzaixianhuikuan_addConfirm.setOnClickListener(this);
    }

    public boolean isMoney(String Num) {
        Pattern p = Pattern
                .compile("^(([1-9]\\d{0,9})|0)(\\.\\d{1,2})?$");
        Matcher m = p.matcher(Num);
        return m.matches();
    }

    //获取当前日期
    public String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new java.util.Date());
        return date;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zaixianhuikuan_addConfirm:
                moneyNum = mzaixianhuikuan_moneyNum.getText().toString().trim();
                if (moneyNum.contains("-")) {
                    Toast.makeText(this, "汇款金额不能是负数", Toast.LENGTH_SHORT).show();
                    return;
                } else if ("".equals(moneyNum)) {
                    Toast.makeText(this, "汇款金额不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!isMoney(moneyNum)) {
                    Toast.makeText(this, "请输入正确金额", Toast.LENGTH_SHORT).show();
                    return;
                } else if (Float.parseFloat(moneyNum) < 0.001) {
                    Toast.makeText(this, "汇款金额不能小于0.01", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(ZaiXianHuiKuanActivity.this, ChoicePayActivity.class);
                intent.putExtra("mAllPrice", moneyNum);
                intent.putExtra("morderName", mzaixianhuikuanbianhao_random.getText());
                startActivity(intent);
                finish();
                break;
        }
    }
}
